#!C:\python25\python.exe

import xml2html
import cgi
import ConfigParser
import string, os, sys
from time import strftime
import result_parser

def template2output():
   template = str(form.getvalue("template"))
   mrn = str(form.getvalue("MRN"))
   output = str(form.getvalue("outputtype"))
   
   templatefilename = templatepath + template + ".xml"
   
   if output == "Browser":
      xml2html_object = xml2html.xml2html(templatefilename)
   elif output == "HtmlFile":
      print "Content-Type: text/html\n\n" 
      time = strftime("%Y-%m-%d-%H-%M-%S")
      reportfilename = reportpath + template + "_" + mrn + "_"+ time + ".html"
      xml2html_object = xml2html.xml2html(templatefilename,reportfilename)
      print "<BR><B>Report has been saved into "+ reportfilename + "</B>"
   else:
      print "Content-Type: text/html\n\n" 
      time = strftime("%Y-%m-%d-%H-%M-%S")
      resultfilename = resultpath + template + "_" + mrn + "_"+ time + ".xml"
      result_parser_object = result_parser.result_parser(templatefilename,resultfilename)
      print "<BR><B>Report has been saved into "+ resultfilename + "</B>"

   
def xml2output():
   result = str(form.getvalue("result"))
   output = str(form.getvalue("outputtype"))
   resultfilename = resultpath + result + ".xml"
   
   if output == "Browser":
      xml2html_object = xml2html.xml2html(resultfilename)
   else:
      print "Content-Type: text/html\n\n" 
      reportfilename = reportpath + result + ".html"
      xml2html_object = xml2html.xml2html(resultfilename,reportfilename)
      print "<BR><B>Report has been saved into "+ reportfilename + "</B>"


#run from here
cfg = ConfigParser.ConfigParser()
cfg.read("path.cfg")
templatepath = cfg.get("template", "path")
reportpath = cfg.get("report", "path")
resultpath = cfg.get("result", "path")

form = cgi.FieldStorage()

if form.getvalue("template") is not None:
   template2output()
else:
   xml2output()
